# -*- coding:utf-8 -*-
# @FileName  : FwCSVOpts.py
# @Time      : 2021/6/22 10:21
# @Author    : xu.shibo
# @Function  :
from robot.api import logger
import os

try:
    import csv
except Exception as e:
    print(e)
    os.system("python -m pip install csv")
    import csv

try:
    import pandas as pd
except Exception as e:
    print(e)
    os.system("python -m pip install pandas")
    import pandas as pd


class FwCSVOpts(object):
    def __init__(self, file_path):
        self.path = file_path
        self.df = self._read_csv()

    def _read_csv(self):
        df = pd.read_csv(self.path)
        return df

    def drop_columns(self, *title):
        for column_title in title:
            try:
                self.df.drop(column_title, axis=1, inplace=True)
            except Exception as e:
                print(e)
                continue
        self._save_file()

    def get_columns(self):
        return self.df.columns

    def deal_str(self, data):
        data = str(data) + "\t"
        return data

    def add_columns(self, **kwargs):
        try:
            data = self._read_csv()
            for k, v in kwargs.items():
                data[k] = pd.DataFrame(list(v))
            data.to_csv(self.path, mode='w', index=False, encoding="utf-8")
        except Exception as e:
            print(e)

    def add_column(self, kwargs: dict, index):
        pass

    def _save_file(self, sep: str = ",", mode: str = "w", columns=None, header=True, index=True, index_label=None,
                   encoding=None, ):
        self.df.to_csv(self.path, index=False, encoding="utf-8")

    def test(self):
        pd.DataFrame()


if __name__ == "__main__":
    file_path = r"F:\AutoTest\V90E-MTN-CD\Case\data\Download\StarterPackNo4gBatchService_011_Request_202106211028248.csv"
    csvOpts = FwCSVOpts(file_path)
    # print(csvOpts.get_columns())
    # csvOpts.drop_column('xu')

    # df_data1 = pd.DataFrame(data1)
    # df_data1.to_csv(file_path, mode='a', header=False, index=False, index_label="-1")
    # csvOpts.add_columns(xu13=["xu11", "test"], xu14 = ["sdfs", "rers"])

    df = csvOpts._read_csv()
    print(df["ServiceAccountID"].tolist())
